<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class Admin_UserController extends Zend_Controller_Action
{

    private $_userService;
    private $_groupService;

    public function init()
    {
        $this->_userService = $this->_helper->getService('Core_Service_User');
        $this->_groupService = $this->_helper->getService('Core_Service_Group');
    }

    public function indexAction()
    {
        $filterContent = array(
             'all'          => 'Alle',
             'confirmed'    => 'Bekreftede',
             'unconfirmed'  => 'Ubekreftede',
         );

         $filterForm = $this->view->filterForm = $this->view->filterForm($filterContent);
         $filterForm->setAction($this->view->url(array('module' => 'admin', 'controller' => 'user'), null, true));

         $request = $this->getRequest();
         if ($request->isGet() && $this->_getParam('filter', false) && $filterForm->isValid($request->getQuery())) {
             $value = $filterForm->getValue('filter');
             switch ($value) {
                 case 'all':
                     $this->view->users = $this->_userService->fetchUsersAll(false);
                     break;
                case 'confirmed':
                    $this->view->users = $this->_userService->fetchUsersOnlyConfirmed(false);
                    break;
                case 'unconfirmed':
                    $this->view->users = $this->_userService->fetchUsersOnlyUnconfirmed(false);
                    break;
             }
         }

         if (!isset($this->view->users)) {
             $this->view->users = $this->_userService->fetchUsersAll(false);
         }
  }

    public function deleteAction()
    {
        $this->_helper->acl()->isAllowed('service:user', 'delete');

        $userId = $this->_getParam('id', false);

        $validator = new Zend_Validate_Int();
        if (!$userId || !$validator->isValid($userId)) {
            throw new RedCross_Exception_ValidationFailed();
        }
        $userId = (int) $userId;

    $user = $this->_userService->fetch($userId);
        if (!$user) {
            throw new RedCross_Exception_NotFound();
        }

    $deleteForm = new Admin_Form_User_Delete();

    $request = $this->getRequest();
    if($request->isPost() && $deleteForm->isValid($request->getPost())) {
      $this->_userService->deleteUser($userId);

            $this->view->flashMessage()->addMessage('Brukeren ble slettet', RedCross_View_Helper_FlashMessage::SUCCESS);
      $this->_helper->redirector('index');
    }

    $this->view->deleteForm = $deleteForm;
    }

    public function confirmAction()
    {
        $this->_helper->acl()->isAllowed('service:user', 'confirm');

        $this->_helper->viewRenderer->setNoRender(true);

        $userId = $this->_getParam('id', false);

        $request = $this->getRequest();
        $validator = new Zend_Validate_Int();
        if (!$userId || !$validator->isValid($userId)) {
            throw new RedCross_Exception_ValidationFailed('Ugyldig parameter angitt');
        }
        $userId = (int)$userId;

        $resultConfirm = $this->_userService->confirmUser($userId);

        if ($resultConfirm) {
            $this->view->flashMessage()->addMessage('Brukeren ble bekreftet', RedCross_View_Helper_FlashMessage::SUCCESS);
        } else {
            $this->view->flashMessage()->addMessage('Brukeren er allerede bekreftet', RedCross_View_Helper_FlashMessage::ERROR);
        }
        return $this->_helper->redirector('index');
    }
}
